﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace WangMi.Common.DBHelper
{
    public class SqlMapperHelper
    {
        public static ISqlMapper Mapper
        {
            get
            {
                if (_mapper != null)
                {
                    return _mapper;
                }
                else
                {
                    //这里需要三个参数:用户名；密码；数据库名
                    //用户名；密码从公共配置里面获取
                    //数据库名根据实际虚拟目录名来获取
                    DomSqlMapBuilder builder = new DomSqlMapBuilder();
                    Assembly assembly = Assembly.Load("WangMi.DataMapping");
                    Stream stream = assembly.GetManifestResourceStream("WangMi.DataMapping.sqlmap.config");
                    //------------------------------以下配置为正常运行配置------------------------------------
                    //if (HttpContext.Current.Session["ISqlMapper"] == null)
                    //{

                    //    string conn = ConfigurationManager.AppSettings["MsSql"].ToString();                        
                    //    _mapper = builder.Configure("sqlmap.config");
                    //    _mapper.DataSource.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["MsSql"];
                    //    IBatisNet.DataMapper.Configuration.DomSqlMapBuilder domBuilder = new IBatisNet.DataMapper.Configuration.DomSqlMapBuilder();

                    //}
                    //else
                    //{
                    //    _mapper = (ISqlMapper)HttpContext.Current.Session["ISqlMapper"];
                    //}
                    //----------------------------------------------------------------------------------------

                    //-------------------------------单元测试配置---------------------------------------------
                    //string conn = ConfigurationManager.AppSettings["MsSql"].ToString();
                    _mapper = builder.Configure(stream);
                    //_mapper.DataSource.ConnectionString = "server=localhost;database="+Common.PubObj.GetVPath+";uid=sa;pwd=xap001;";
                    #region 根据虚拟目录名读取数据库
                    //if (ConfigurationManager.AppSettings["Multi_user"].ToString() == "1")
                    //{
                    //    _mapper.DataSource.ConnectionString = "server=localhost;database=" + WebLib.HttpModules.NewRequest.Current.Subfolder + ";uid=sa;pwd=sa;";
                    //}
                    //else
                    //{
                    //    _mapper.DataSource.ConnectionString = "server=localhost;database=" + Common.PubObj.GetVPath + ";uid=sa;pwd=sa;";
                    //}
                    #endregion
                    IBatisNet.DataMapper.Configuration.DomSqlMapBuilder domBuilder = new IBatisNet.DataMapper.Configuration.DomSqlMapBuilder();
                    ((IBatisNet.Common.DbProvider)(_mapper.DataSource.DbProvider)).Initialize();
                    //---------------------------------------------------------------------------------------
                    //多线程
                    _mapper.SessionStore = new IBatisNet.DataMapper.SessionStore.HybridWebThreadSessionStore(_mapper.Id);
                    return _mapper;
                }
            }
            set { _mapper = value; }
        }
        protected static ISqlMapper _mapper = null;
    }
}
